package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.DictTypeConstants;
import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.UserEntity;
import com.bsg.upm.entity.UserRoleEntity;

/**
 * 用户角色业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class UserRoleCheck extends BaseCheck {

	/**
	 * 用户角色新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户角色编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 角色删除时检查
	 * 
	 * @param role
	 *            用户角色对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(UserRoleEntity role) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (role == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "角色"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 角色编码非空检查
		String code = (String) paramMap.get("code");
		if (StringUtils.isBlank(code)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色编码"));
			return chkRS;
		}

		// 角色名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 16) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "角色名称", 16));
			return chkRS;
		}

		// 角色级别非空和有效性检查
		Integer level = (Integer) paramMap.get("level");
		if (level == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色级别"));
			return chkRS;
		}
		if (level < 1 || level > 99) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "角色级别", 1, 99));
			return chkRS;
		}

		// 数据范围非空检查
		String dataScope = (String) paramMap.get("dataScope");
		if (StringUtils.isBlank(dataScope)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "数据范围"));
			return chkRS;
		}

		// 角色描述非空检查
		String description = (String) paramMap.get("description");
		if (StringUtils.trim(description).length() > 256) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "角色描述", 256));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 角色编码重复性检查
		String code = (String) paramMap.get("code");
		if (userRoleDao.countByCode(code) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "角色编码"));
			return chkRS;
		}

		// 角色名称重复性检查
		String name = (String) paramMap.get("name");
		if (userRoleDao.countByName(name) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "角色名称"));
			return chkRS;
		}

		// 数据范围存在性检查
		String dataScope = (String) paramMap.get("dataScope");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.DATA_SCOPE, dataScope) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "数据范围"));
			return chkRS;
		}

		UserEntity user = getUser();
		Integer level = (Integer) paramMap.get("level");
		if (level.intValue() <= user.getUserRole().getLevel().intValue()) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("角色等级不能小于等于自己本身");
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 角色名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 16) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "角色名称", 16));
			return chkRS;
		}

		// 角色等级非空和有效性检查
		Integer level = (Integer) paramMap.get("level");
		if (level == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色级别"));
			return chkRS;
		}
		if (level < 1 || level > 99) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "角色级别", 1, 99));
			return chkRS;
		}

		// 数据范围非空检查
		String dataScope = (String) paramMap.get("dataScope");
		if (StringUtils.isBlank(dataScope)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "数据范围"));
			return chkRS;
		}

		// 角色描述长度检查
		String description = (String) paramMap.get("description");
		if (StringUtils.trim(description).length() > 256) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "角色描述", 256));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 角色存在性检查
		String code = (String) paramMap.get("code");
		UserRoleEntity role = userRoleDao.get(code);
		if (role == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "角色编码"));
			return chkRS;
		}

		// 角色名称重复性检查
		String name = (String) paramMap.get("name");
		if (!role.getName().equals(name)) {
			if (userRoleDao.countByName(name) > 0) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "角色名称"));
				return chkRS;
			}
		}

		// 数据范围存在性检查
		String dataScope = (String) paramMap.get("dataScope");
		if (dictTypeCache.getDictFromCache(DictTypeConstants.DATA_SCOPE, dataScope) == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "数据范围"));
			return chkRS;
		}

		Integer level = (Integer) paramMap.get("level");
		if (!role.getLevel().equals(level)) {
			UserEntity user = getUser();
			if (level.intValue() <= user.getUserRole().getLevel().intValue()) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("角色等级不能小于等于自己本身");
				return chkRS;
			}
		}

		return chkRS;
	}

}
